package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin.devtypes;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.AddressType;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Alarm;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.CompoundCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.DeviceType;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Field;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SimpleCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;

public class DeviceTypeBean {

	private DeviceType devType;
	private SystemService sysServ;
	
	public DeviceTypeBean(DeviceType dev, SystemService serv) {
		devType = dev;
		sysServ = serv;
	}

	public DeviceTypeBean() {
		devType = new DeviceType();
	}

	public DeviceType getDevType() {
		return devType;
	}

	public void setDevType(DeviceType devType) {
		this.devType = devType;
	}

	public SystemService getSysServ() {
		return sysServ;
	}

	public void setSysServ(SystemService sysServ) {
		this.sysServ = sysServ;
	}

	public List<FieldBean> getFields(){
		return FieldBean.wrap(devType.getFields());
	}
	
	 public List<SimpleCommandBean> getSimpleCommands(){
		return SimpleCommandBean.wrap(devType.getSimpleCommands());
	}
	
	 public List<CompoundCommandBean> getCompoundCommands(){
		return CompoundCommandBean.wrap(devType.getCompoundCommands());
	}
	
	 public List<AlarmBean> getAlarms(){
		return AlarmBean.wrap(devType.getAlarms());
	}
	 
	public String getName() {
		return devType.getName();
	}

	public void setName(String name) {
		devType.setName(name);
	}
	
	public void edit(){
		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.put("selectedType", this);
	}
	
	public void delete(){
		sysServ.deleteDeviceType(devType);
	}
	
	public void newField(){
		Field newF = new Field();
		newF.setAddress(0);
		newF.setAddressType(AddressType.COIL);
		newF.setBitOffset(0);
		newF.setBitType(true);
		newF.setDeviceType(devType);
		newF.setName("XXX");
		devType.getFields().add(newF);
	}
	
	public void newSimpleCommand(){
		SimpleCommand newCmd = new SimpleCommand();
		newCmd.setAddress(0);
		newCmd.setAddressType(AddressType.COIL);
		newCmd.setBitOffset(0);
		newCmd.setDeviceType(devType);
		newCmd.setName("XXX");
		devType.getSimpleCommands().add(newCmd);
	}
	
	public void newCompoundCommand(){
		CompoundCommand newCmd = new CompoundCommand();
		newCmd.setAddress(0);
		newCmd.setDeviceType(devType);
		newCmd.setName("XXX");
		devType.getCompoundCommands().add(newCmd);
	}
	
	public void newAlarm(){
		if(devType.getFields().isEmpty())
			return;
		Alarm newAlarm = new Alarm();
		newAlarm.setName("XXX");
		newAlarm.setField(devType.getFields().get(0));
		newAlarm.setOperator(Alarm.Operator.EQUAL);
		newAlarm.setThreshold(0);
		newAlarm.setDeviceType(devType);
		devType.getAlarms().add(newAlarm);
	}
	
	public void saveChanges(){
		sysServ.saveType(devType);
	}
	
	public void delete(FieldBean fieldBean){
		fieldBean.getField().setDeviceType(null);
		devType.getFields().remove(fieldBean.getField());
	}
	
	public void delete(SimpleCommandBean cmd){
		cmd.getCommand().setDeviceType(null);
		devType.getSimpleCommands().remove(cmd.getCommand());
	}
	
	public void delete(CompoundCommandBean cmd){
		cmd.getCommand().setDeviceType(null);
		devType.getCompoundCommands().remove(cmd.getCommand());
	}
	
	public void delete(AlarmBean alarm){
		alarm.getAlarm().setDeviceType(null);
		devType.getAlarms().remove(alarm.getAlarm());
	}
	
	public boolean isAlarmdisabled(){
		return devType.getFields().isEmpty();
	}
	
	public static List<DeviceTypeBean> wrap(List<DeviceType> sysDevs, SystemService serv) {
		List<DeviceTypeBean> ret = new ArrayList<DeviceTypeBean>();

		for (DeviceType dev : sysDevs) {
			ret.add(new DeviceTypeBean(dev,serv));
		}
		return ret;
	}
}
